package com.baichang.taoshunda.controller;


import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.AppVersionService;
import com.baichang.taoshunda.service.CommonProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Hashtable;

/**
 * Created by Administrator on 2017/8/23 0023.
 */

@Controller
@RequestMapping(ControllerConstants.APP)
public class AppController extends WebBaseController {


    @Autowired
    private AppVersionService appVersionService;

    @Autowired
    private CommonProblemService commonProblemService;

    /**
     * @return
     */
    @RequestMapping("/findApp")
    public String findApp() {
        return "/taoshunda/app/app";
    }

    /**
     * @return
     */
    @RequestMapping("/findProblem")
    public String findProblem() {
        return "/taoshunda/problem/problem";
    }


    /**
     * app列表页面
     */
    @RequestMapping("/queryApp")
    public void findAllParam(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                return appVersionService.queryAllApp(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 修改版本信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/editById")
    public void editById(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                int version = getInt(request, "version", 0);
                String url = getString(request, "url", "");
                String tagversion = getString(request, "tagversion", "");
                String updateInfo = getString(request, "updateInfo", "");
                int isForce = getInt(request, "isForce", 0);
                return appVersionService.editById(id, version, tagversion, url, updateInfo, isForce);
            }
        });
    }

    /**
     * 进行删除
     */
    @RequestMapping("delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                return appVersionService.delete(id);
            }
        });
    }

    /**
     * 添加问题
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addProblem")
    public void addProblem(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String question = getString(request, "question", "");
                String answer = getString(request, "answer", "");
                Integer type = getInt(request, "type", 0);
                return commonProblemService.addProblem(question, answer, type);
            }
        });
    }

    /**
     * 修改问题
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateProblem")
    public void updateProblem(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String question = getString(request, "question", "");
                String answer = getString(request, "answer", "");
                Integer type = getInt(request, "type", 0);
                return commonProblemService.updateProblem(id, question, answer, type);
            }
        });
    }

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/deleteProblem")
    public void deleteProblem(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return commonProblemService.deleteProblem(id);
            }
        });
    }

    /**
     * 查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryProblem")
    public void queryProblem(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                return commonProblemService.queryProblem(nowPage, pageSize);
            }
        });
    }
}
